#![windows_subsystem = "windows"]

slint::include_modules!();

mod parse;
use parse::parser::{chem_parse, parse};
use slint::{Model, ModelRc, SharedString, StandardListViewItem, VecModel};

fn main() {
    let app = DaltonMain::new().unwrap();

    let list = app.get_list();

    app.on_calc( move |s| {
        if s.is_empty() {return;}
        let parse = if s.contains(&['+', '-', '*', '/', '<', '>']) {parse} else {chem_parse};
        let ans:SharedString = match parse(&s) {
            Ok(mw) => {format!("{:.4}", mw)},
            Err(e) => {e},
        }.into();
        let list:&VecModel<ModelRc<StandardListViewItem>> = list.as_any().downcast_ref().unwrap();
        let item = ModelRc::from([StandardListViewItem::from(s), StandardListViewItem::from(ans)]);
        list.push(item);
    });

    app.run().unwrap();
}
